// pages/address/addressbook/addressbook.js
const request = require('@/utils/request.js')
const constants = require('@/utils/khyConstants.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],
    prePage: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app = getApp();
    let data = getApp().globalData.pageData[constants.page_addressbook]
    if (data && data['prePage']) {
      this.setData({
        prePage: data['prePage'],
        edit:app.globalData.nginxIp+'/img/addressbook_edit.png',
        addressbook_delete:app.globalData.nginxIp+'/img/addressbook_delete.png'
      })
    }
    
    
  },

  getPageData() {
    let user = getApp().getUserInfo()
    request.get('/UserController/getUserAdress', {userNo: user.userNo}).then(res => {
      if (res.data.code == constants.resultSuccessCode
        && res.data.result) {
          let data = res.data.result || []
          this.setData({
            addressList: data
          })
      }
    })
  },

  onAddressClick(e) {
    let address = e.currentTarget.dataset.address
    if (address) {
      let data = Object.assign({}, address, {prePage: this.data.prePage})
      getApp().globalData.pageData[constants.order_address] = data
      wx.navigateBack({})
      // wx.navigateTo({
      //   url: '/pages/order/order',
      // })
    }
  },

  newaddress() {
    getApp().globalData.pageData[constants.page_newaddress] = {}
    wx.navigateTo({
      url: '/pages/address/newaddress/newaddress',
    })
  },

  addressEdit(e) {
    let address = e.currentTarget.dataset.address
    if (address) {
      getApp().globalData.pageData[constants.page_newaddress] = address
      wx.navigateTo({
        url: '/pages/address/newaddress/newaddress',
      })
    }

  },

  addressDelete(e) {
    let keyId = e.currentTarget.dataset.key
    if (keyId) {
      request.get('/UserController/deleteAddress', {keyId: keyId}).then(res => {
        if (res.data.code == constants.resultSuccessCode) {
          this.getPageData()
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getPageData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})